<?php

/**
 * description: 递归菜单
 * @author: wuyanwen(2016年8月7日)
 * @param unknown $array
 * @param number $fid
 * @param number $level
 * @param number $type 1:顺序菜单 2树状菜单
 * @return multitype:number
 */
function get_column($array, $type = 1, $fid = 0, $level = 0) {
    $column = array();
    if ($type == 2)
        foreach ($array as $key => $vo) {
            if ($vo['pid'] == $fid) {
                $vo['level'] = $level;
                $column[$key] = $vo;
                $column [$key][$vo['id']] = get_column($array, $type = 2, $vo['id'], $level + 1);
            }
        } else {
        foreach ($array as $key => $vo) {
            if ($vo['pid'] == $fid) {
                $vo['level'] = $level;
                $column[] = $vo;
                $column = array_merge($column, get_column($array, $type = 1, $vo['id'], $level + 1));
            }
        }
    }


    return $column;
}

/**
 * 活动条件类型
 */
function promotionType($type = 1) {
    if ($type == 1) {
        return '限制类';
    }
    if ($type == 2) {
        return '条件类';
    }
    if ($type == 3) {
        return '结果类';
    }
}

/**
 *  单据编号生成
 *
 */
function createSn($type = 'B') {
    $sn = $type . date('Ymd', time());
    $sn1 = date('His', time());
    $max = M('bill')->where(" bill_sn like '%$sn%' ")->order('bill_id desc')->getField('bill_sn');
    if (empty($max)) {
        return $sn . $sn1 . '00001';
    } else {
        $maxsn = intval(substr($max, -5)) + 1;
        if ($maxsn < 10) {
            $maxsn = '0000' . $maxsn;
        } else if ($maxsn < 100) {
            $maxsn = '000' . $maxsn;
        } else if ($maxsn < 1000) {
            $maxsn = '00' . $maxsn;
        } else if ($maxsn < 10000) {
            $maxsn = '0' . $maxsn;
        }
        return $sn . $sn1 . $maxsn;
    }
}

/**
 * 目录创建
 */
function createFolder($dir) {
    if (is_dir($dir) || @mkdir($dir, 0777)) {
        // echo '';
    } else {
        $dirArr = explode('/', $dir);
        array_pop($dirArr);
        $newDir = implode('/', $dirArr);
        createFolder($newDir);
        if (@mkdir($dir, 0777)) {
            // echo 'dd';
        }
    }
}

/**
 * 上传文件记录 CONTROLLER_NAME,ACTION_NAME
 */
function recodeUpload($file, $dir, $table, $fields) {
    $data['controller'] = CONTROLLER_NAME;
    $data['action'] = ACTION_NAME;
    $data['file_name'] = $file;
    $data['table'] = $table;
    $data['fileds'] = $fields;
    $data['dir'] = $dir;
    M('upload_files', 'ecs_')->add($data);
}

/**
 *  记录日志
 *
 */
function adminLog($content) {
    $dir = 'Public/log/';
    $info = session('user_info');
    createFolder($dir);
    $file = $dir . date('Y-m-d_', time()) . CONTROLLER_NAME . '.txt';
    $content = ACTION_NAME . ' ' . date('H:i:s', time()) . ' ' . $info['user_name'] . ' ' . $content . "\r\n";
    file_put_contents($file, $content, FILE_APPEND);
}

/**
 *  查询下属部门ID
 */
function getDepartId($id = '', &$arr = array()) {
    $idArr = M('user_depart')->where("up_depart_id in ($id) ")->getField('depart_id', true);
    $idStr = implode(',', $idArr);
    if (!empty($idStr)) {
        $arr[] = $idStr;
        getDepartId($idStr, $arr);
    }
    $str = implode(',', $arr);
    return $str;
}

function getStatusName($status=0){
    if($status=0){
        return '未处理';
    }
    if($status=1){
        return '已派单';
    }
    if($status=2){
        return '已开始';
    }
    if($status=3){
        return '已取消';
    }
    if($status=4){
        return '待支付';
    }
    if($status=5){
        return '待评价';
    }
    if($status=6){
        return '已完成';
    }
    if($status=7){
        return '已终止';
    }
    if($status=8){
        return '已报价';
    }
    if($status=9){
        return '已下订';
    }
    if($status=10){
        return '收现';
    }
}

function formatAttr($attr = '') {
    $attr = str_replace('规格:', '', $attr);
    $attr = str_replace('图色', '', $attr);
    $attr = str_replace('00', '', $attr);
    return $attr;
}
